<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>  
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:include value="/comm/header.jsp" />
<div class="wrap1">
    	<div class="blank1"></div>
        <div class="outbox">
<!--1 start-->
			<div>
        		<div class="wrap_left">
            
                	<span class="tab7_tt">我的代购车</span>
              
            	</div>
            	<div class="wrap_right">
            		<div>
						<div class="pbox6">
            				<ul>
                			<li class="bg1">&nbsp;</li>
                			<li class="s1"><span class="tt">1.&nbsp;&nbsp;查看代购车</span></li>
                    		<li class="bg2">&nbsp;<li>
                   			<li class="s2"><span class="tt">2.&nbsp;&nbsp;确认订单信息</span></li>
                    		<li class="bg3">&nbsp;<li>
                    		<li class="s2"><span class="tt">2.&nbsp;&nbsp;托管代购款到代购岛平台</span></li>
                    		<li class="bg3">&nbsp;<li>
                    		<li class="s2"><span class="tt">3.&nbsp;&nbsp;经纪人发货</span></li>								
							<li class="bg3">&nbsp;</li>
                    		<li class="s2"><span class="tt">3.&nbsp;&nbsp;确定收货</span></li>
                    		<li class="s2"><span class="tt">5.&nbsp;&nbsp;评价</span></li>
							<li class="bg4">&nbsp;</li>
                			</ul>
                        </div>
            		</div>
                    
                </div>
                <div class="clear"></div>
            </div>
            
            
<!--1 end -->

 		<div class="pbox7"><div class="tips4">查看代购车</div></div>   
<!--订单确认列表开始-->
        <div class="pbox12" id="carts">
                <ul>
                	<li class="t1 tt">&nbsp;</li>
                    <li class="t2 tt"><span>商品</span></li>
                    <li class="t3 tt"><span>价格</span></li>
                    <li class="t4 tt"><span>服务费</span></li>
                    <li class="t5 tt"><span>数量</span></li>
                    <li class="t6 tt"><span>小计</span></li>
                </ul>
				<s:iterator value="brokers" id="broker">
					<div class="t8" id="broker<s:property value='userid' />"><input id="brokerChk<s:property value='userid' />" name="brokerChk" type="checkbox" checked value="<s:property value='userid' />"/>&nbsp;&nbsp;<strong>经纪人：</strong><a href='#'><s:property value='nickName' /></a></div>
					<s:iterator value="cartsMap.get(#broker.userid).items">
						 <ul class="ttbox" id="item<s:property value='userid'/><s:property value='value.canBuyModel.canId' />" name="item<s:property value='userid'/>">
							<li class="t1">
								<div class="pbox1_list_pic">
									<div>
										<p>
											<img src="<s:property value='value.canBuyModel.images[0]' />" onload='javascript:DrawImage(this,50,50)'/>
										</p>
									</div>
								</div>
							</li>
							 <li class="t2 t2_a" style="width:">
							 	<span>
							 		<input name="itemChk" broker="<s:property value='userid' />" type="checkbox" checked value="<s:property value='userid'/>,<s:property value='value.canBuyModel.canId' />,<s:property value='value.subtotal' />"/>&nbsp;&nbsp;<a href="canBuyDetail.htm?canBuyModel.canId=<s:property value='value.canBuyModel.canId' />"><s:property value='value.canBuyModel.goodsName '/></a>
							 	</span> 
							 </li>
							 <li class="t3"><span class="span18">￥<s:property value='value.canBuyModel.price' /></span></li>
							 <li class="t4"><span class="span19">￥<s:property value='value.canBuyModel.serviceFee' /></span></li>
							 <li class="t5"><span><s:property value='value.number' /></span></li>
							 <li class="t6"><span class="span18">￥<s:property value="value.subtotal" /></span></li>
							 <li class="t7">
							 	<span><a href="javascript:delItem(<s:property value='userid'/>, <s:property value='value.canBuyModel.canId' />)">删除</a></span>
							 </li>
					 	</ul>
					</s:iterator>
				</s:iterator>
         </div>
         
 <!--订单确认列表结束-->  
 		<div class="blank1"></div>
		<div class="pbox12">
            	<div class="wrap_left cbox1">
            		<input id="selectAll" name="selectAll" type="checkbox" checked value="1"/>&nbsp;&nbsp;&nbsp;全选
            		&nbsp;&nbsp;&nbsp;<a href='javascript:delItems()'>批量删除</a>
            	</div>
                <div class="wrap_right">
                	<strong>商品总价（不含运费）：</strong><span class="span2" id="total">￥<s:property value='#session.cart.total' /></span>
                	&nbsp;&nbsp;&nbsp;
                	<span  class="but_sub1">
                		<s:if test="#session.cart.size == 0">
                			<a href="javascript:void(0)" id="settlement">现在结算</a>	
                		</s:if>
                		<s:else>
                			<a href="javascript:settlement()" id="settlement">现在结算</a>	
                		</s:else>
                	</span>
                </div>
                <div class="clear"></div>
        </div>
        
	
        
        </div>
</div>

<s:include value="/comm/bottom.jsp"/>

<script type="text/javascript">

	$("[name=brokerChk]").each(function() {
		$(this).click(function(){selectBroker($(this).attr("checked"), $(this).val());});
	});

	$("[name=itemChk]").each(function() {
		$(this).click(function() {selectItem($(this).val());});
	});
	
	$("#selectAll").click(selectAll);
	
	function selectBroker(checked, userId) {
		$('[broker=' + userId + ']').attr("checked", checked);
		calcTotal();
	}
	
	function settlement() {
		var items = $('[name=itemChk]:checked');
		var canIds = "";
		for (var i = 0; i < items.length; i++) {
			var tokens = items[i].value.split(",");
			canIds += tokens[1] + ",";
		}
		window.location.href = "settlement.htm?canIds=" + canIds;
	}

	function selectItem(value) {
		var userId = value.split(",")[0];
		if ($("[broker=" + userId + "]:checked").length == $("[broker=" + userId + "]").length) {
			$("#brokerChk" + userId).attr("checked", true);
		} else {
	         $("#brokerChk" + userId).attr("checked", false);
		}
	
		if ($("[name=itemChk]:checked").length == $("[name=itemChk]").length) {
			$("#selectAll").attr("checked", true);
		}
		else {
			$("#selectAll").attr("checked", false);
		}
		calcTotal();
	
	}

	function selectAll() {
		var checked = $("#selectAll").attr("checked");
		$("[name=itemChk]").attr("checked", checked);
		$("[name=brokerChk]").attr("checked", checked);
		calcTotal();
	}
	
	function calcTotal() {
		var items = $('[name=itemChk]');	
		var total = 0;
		for (var i = 0; i < items.length; i++) {
			if (items[i].checked) {
				total += parseFloat(items[i].value.split(",")[2]);
			}
		}
		$("#total").html("￥" + total.toFixed(2));
		if (total == 0) {
			$("#settlement").attr("href", "#");
		}
		else {
			$("#settlement").attr("href", "javascript:settlement();");
		}
	}
	
	function delItems() {
		var items = $('[name=itemChk]:checked');
		if (items.length == 0) {
			alert("请选择代购信息");
		}
		else {
			var confirm = window.confirm("确定要删除选中的代购信息吗？");
			if (confirm) {
				var canIds = "";
				for (var i = 0; i < items.length; i++) {
					var tokens = items[i].value.split(",");
					canIds += tokens[1] + ",";
					removeItem(tokens[0], tokens[1]);
				}
				calcTotal();
				doAjax(canIds);
			}
		}
		
	}
	
	function delItem(userId, canId) {
		remoteItem(userId, canId);
		calcTotal();
		doAjax(canId);
	}
	
	function remoteItem(userId, canId) {
		var itemId = "item" + userId + canId;
		var brokerId = "broker" + userId;
		var itemName = "item" + userId;
		$("#" + itemId).remove();
		var items = $('[name=' + itemName  + ']');
		if (items.length == 0) {
			$("#" + brokerId).remove();
		}
	}
	
	function doAjax(canIds) {
		$.ajax({
			url:'delItems.htm',
			type:'post',
			data:{"canIds":canIds}
		});	
	}
	
	
</script>